﻿Imports System.Linq

Public Class Linq_Bestelling

    Public Function getBestellingenVanKlant(ByVal klantId As Integer) As List(Of BestellingBean)
        Dim SaktieDBDataContext1 = New SaktieDBDataContext()

        Dim bestellingen As New List(Of BestellingBean)

        Dim BestellingQuery = From Bestel In SaktieDBDataContext1.Bestellings _
                              Where Bestel.klant_id = klantId
                              Take 25

        Dim bestelling As New BestellingBean

        For Each b In BestellingQuery
            bestelling = New BestellingBean
            bestelling.besID = b.best_id
            bestelling.KlantID = b.klant_id
            bestelling.ProductID = b.Product.product_naam
            bestelling.Aantal = b.aantal
            bestellingen.Add(bestelling)
        Next

        Return bestellingen
    End Function

    Public Function createBestelling(ByVal b As BestellingBean) As Boolean
        Dim dbc = New SaktieDBDataContext()
        Dim inserted As Boolean = False
        Dim bb As New Bestelling

        bb.best_id = b.besID
        bb.klant_id = b.KlantID
        bb.product_id = Integer.Parse(b.ProductID)
        bb.aantal = b.Aantal

        dbc.Bestellings.InsertOnSubmit(bb)
        dbc.SubmitChanges()
        inserted = True
        Return inserted
    End Function

    Public Function createBestellingId() As Integer
        'Hier moet een query komen die de laatste bestelling id ophaalt en deze met 1 verhoogt en returnd
        Dim SaktieDBDataContext1 = New SaktieDBDataContext()
        Dim klanten As New List(Of KlantBean)
        Dim IdQuery = From Bestelling In SaktieDBDataContext1.Bestellings
                      Order By Bestelling.best_id Descending
                      Select Bestelling.best_id

        If IdQuery.Count < 1 Then
            'Gebruiker bestaat niet
            Return 1000
        Else
            'Gebruiker bestaat al
            Dim id As Integer = IdQuery.First
            id += 1
            Return id
        End If
    End Function
End Class
